package com.lastproject.web.bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.event.ActionEvent;

import com.lastproject.dto.EmailGroupDTO;
import com.lastproject.dto.UserDTO;
import com.lastproject.ejb.dao.EmailGroupDAORemote;
import com.lastproject.ejb.dao.UserDAORemote;

public class ManageGroupsBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7007086206698071912L;

	private int idUser;

	private boolean alreadyExists;
	
	private boolean error;

	@EJB
	EmailGroupDAORemote emailGroupDAORemote;

	@EJB
	UserDAORemote userDAORemote;

	EmailGroupDTO emailGroupDTO;

	UserDTO userDTO;

	List<EmailGroupDTO> groups = new ArrayList<EmailGroupDTO>();

	List<UserDTO> userDTOList = new ArrayList<UserDTO>();

	public String edit(ActionEvent event) {
		emailGroupDTO = (EmailGroupDTO) event.getComponent().getAttributes()
				.get("currentGroup");
		return "";
	}

	public String editGroup() {
		return "addUserToGroup.xhtml";
	}

	public String create() {
		List<EmailGroupDTO> allGroups = emailGroupDAORemote.findAll();
		EmailGroupDTO newGroup = new EmailGroupDTO();
		newGroup.setName(emailGroupDTO.getName());
		alreadyExists = false;
		for (EmailGroupDTO e : allGroups) {
			if (e.getName().equals(newGroup.getName()))
				alreadyExists = true;
		}

		if (alreadyExists == true) {
			emailGroupDTO.setName("");
			return "groupCreate.xhtml";
		}

		emailGroupDAORemote.create(newGroup);

		emailGroupDTO = emailGroupDAORemote.findLastByName(emailGroupDTO
				.getName());

		emailGroupDTO.setName("");
		return "manageGroups.xhtml";
	}

	public String addUser() {
		if (idUser != 0) {
			error = false;
			userDTO = userDAORemote.findById(idUser);

			List<UserDTO> users = emailGroupDTO.getUsers();
			if (users == null) {
				users = new ArrayList<UserDTO>();
			}
			users.add(userDTO);
			emailGroupDTO.setUsers(users);
			emailGroupDAORemote.edit(emailGroupDTO);
			List<UserDTO> newList = new ArrayList<UserDTO>();
			for (int i = 0; i < userDTOList.size(); i++) {
				if (userDTOList.get(i).getId() != userDTO.getId())
					newList.add(userDTOList.get(i));
			}
			userDTOList = newList;
		}
		else{
			error = true;
		}
		return "addUserToGroup.xhtml";
	}

	public String removeUser() {
		if (idUser != 0) {
			error = false;
			userDTO = userDAORemote.findById(idUser);

			List<UserDTO> usersNow = new ArrayList<UserDTO>();
			usersNow = emailGroupDTO.getUsers();

			List<UserDTO> usersNew = new ArrayList<UserDTO>();
			for (UserDTO u : usersNow) {
				if (u.getId() != userDTO.getId())
					usersNew.add(u);
			}

			emailGroupDTO.setUsers(usersNew);
			emailGroupDAORemote.edit(emailGroupDTO);
		}
		else{
			error = true;
		}
		return "removeUserFromGroup.xhtml";
	}
	
	public String back(){
		emailGroupDTO.setName("");
		alreadyExists = false;
		error = false;
		return "manageGroups.xhtml";
	}

	public EmailGroupDTO getEmailGroupDTO() {
		return emailGroupDTO;
	}

	public void setEmailGroupDTO(EmailGroupDTO emailGroupDTO) {
		this.emailGroupDTO = emailGroupDTO;
	}

	public UserDTO getUserDTO() {
		return userDTO;
	}

	public void setUserDTO(UserDTO userDTO) {
		this.userDTO = userDTO;
	}

	public List<EmailGroupDTO> getGroups() {
		return emailGroupDAORemote.findAll();
	}

	public void setGroups(List<EmailGroupDTO> groups) {
		this.groups = groups;
	}

	public int getIdUser() {
		return idUser;
	}

	public void setIdUser(int idUser) {
		this.idUser = idUser;
	}

	public List<UserDTO> getUserDTOList() {
		userDTOList = userDAORemote.getAll();
		List<UserDTO> isInGroupList = new ArrayList<UserDTO>();
		isInGroupList = emailGroupDTO.getUsers();
		List<UserDTO> newList = new ArrayList<UserDTO>();

		if (isInGroupList.size() == 0) {
			return userDTOList;
		}

		for (UserDTO user : userDTOList) {
			boolean is = false;
			for (UserDTO u : isInGroupList) {
				if (user.getId() == u.getId())
					is = true;
			}
			if (is == false)
				newList.add(user);
		}

		return newList;
	}

	public void setUserDTOList(List<UserDTO> userDTOList) {
		this.userDTOList = userDTOList;
	}

	public boolean isAlreadyExists() {
		return alreadyExists;
	}

	public void setAlreadyExists(boolean alreadyExists) {
		this.alreadyExists = alreadyExists;
	}

	public boolean isError() {
		return error;
	}

	public void setError(boolean error) {
		this.error = error;
	}

}
